package com.qingmi.farmer.ui.trolley;

import android.support.v4.view.ViewPager;

import com.luck.picture.lib.rxbus2.RxBus;
import com.qingmi.farmer.R;
import com.qingmi.farmer.databinding.FragmentTrolleyBinding;
import com.wjjun.base.ui.base.TabFragment;

/**
 * 购物车
 * Created by 67535 on 2018/4/28.
 */

public class TrolleyFragment extends TabFragment<TrolleyViewModel, FragmentTrolleyBinding> {

    private boolean showBack = false;

    private BuyTrolleyFragment buyTrolleyFragment;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_trolley;
    }

    @Override
    protected TrolleyViewModel initViewModel() {
        return new TrolleyViewModel();
    }

    @Override
    public void initFragment() {
        if (buyTrolleyFragment == null) {
            buyTrolleyFragment = new BuyTrolleyFragment();
        }

        clearTabs();

        addTab(getString(R.string.txt_buy_trolley_title), buyTrolleyFragment);
    }

    @Override
    protected void initContent() {
        super.initContent();
        viewModel.setShowLeftBtn(showBack);
        viewModel.setTitle(getString(R.string.title_trolley));
        viewModel.addRightText(getString(R.string.txt_manager));
    }


    public void setShowBack(boolean showBack) {
        this.showBack = showBack;
    }

    @Override
    protected void initData() {
        super.initData();
        setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                updateRightTxt();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onToolBarRightTxtClick() {
        super.onToolBarRightTxtClick();
        if (buyTrolleyFragment != null && buyTrolleyFragment.getUserVisibleHint()) {
            buyTrolleyFragment.toggleShowManagerBar();
        }
        updateRightTxt();
    }

    public void updateRightTxt() {
        if (buyTrolleyFragment != null && buyTrolleyFragment.getUserVisibleHint()) {
            if (buyTrolleyFragment.isShowManagerBar()) {
                viewModel.addRightText(getString(R.string.txt_complete));
            } else {
                viewModel.addRightText(getString(R.string.txt_manager));

            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        RxBus.getDefault().unregister(this);
    }
}
